﻿function SearchBox(windowId, options, map) {
    this.windowId_ = windowId;
    this.content_ = options.content;
    this.clickable_ = options.clickable || true;
    this.className_ = options.className || '';
    this.showMode_ = options.showMode || 'center';
    this.close_ = options.close || {};
    this.events_ = options.events || {};

    this.map_ = map;
    this.containerDiv_ = null;
    this.contentDiv_ = null;
}
SearchBox.prototype = new google.maps.OverlayView();
SearchBox.prototype.onAdd = function () {

    this.contentDiv_ = $(this.content_).get(0);
    this.containerDiv_ = jQuery("<div/>").css('border', 'none')
                           .css('border-width', '0px')
                           .css('position', 'absolute')
                           .addClass(this.className_)
                           .get(0);
    $(this.contentDiv_).appendTo($(this.containerDiv_));

    var panes = this.getPanes();
    panes.floatPane.appendChild(this.containerDiv_);

    var infowindow = this;
    jQuery(this.containerDiv_).click(function (e) {
        return false;
    });
    jQuery(this.close_).click(function (e) {
        infowindow.close();
    });

    if (this.clickable_) {
        function newEventPassthru(obj, event) {
            return function () {
                google.maps.event.trigger(obj, event)
            }
        }
        var eventPassthrus = ['click', 'dblclick', 'mousedown', 'mouseup', 'mouseover', 'mouseout'];
        for (var i = 0; i < eventPassthrus.length; i++) {
            var name = eventPassthrus[i];
            google.maps.event.addListener(this.containerDiv_, name, newEventPassthru(this, name));
            if (this.events_[name]) {
                jQuery(this.containerDiv_)[name](this.events_[name]);
            }
        }
    }
}
SearchBox.prototype.onRemove = function () {
    if (this.containerDiv_) {
        this.containerDiv_.parentNode.removeChild(this.containerDiv_);
        this.containerDiv_ = null;
    }
}
SearchBox.prototype.remove = function () {
    this.setMap(null);
    if (this.containerDiv_) {
        this.containerDiv_.parentNode.removeChild(this.containerDiv_);
        this.containerDiv_ = null;
    }
}
SearchBox.prototype.draw = function () {
    var overlayProjection = this.getProjection();
    var position = '';
    var mapBound = this.map_.getBounds();
    var mapNode = jQuery(this.map_.getDiv());

    if (this.showMode_ == 'center') {
        var northEast = overlayProjection.fromLatLngToDivPixel(mapBound.getNorthEast());
        var southWest = overlayProjection.fromLatLngToDivPixel(mapBound.getSouthWest());
        var content = jQuery(this.contentDiv_);
        var container = jQuery(this.containerDiv_);
        container.css('left', southWest.x + (mapNode.width() - content.width()) / 2 + 'px')
                 .css('top', northEast.y + (mapNode.height() - content.height()) / 2 + 'px');
        content.css('top', '0px').css('left', '0px');
    }
}
SearchBox.prototype.open = function (map) {
    this.map_ = map || this.map_;
    this.map_.searchBox_ = this;
    this.setMap(this.map_);
}
SearchBox.prototype.close = function () {
    this.map_.searchBox_ = null;
    this.remove();
}

google.maps.Map.prototype.setSearchBox = function (searchBox) {
    this.clearSearchBox();
    searchBox.open(this);
}
google.maps.Map.prototype.clearSearchBox = function () {
    if (this.searchBox_) {
        this.searchBox_.close();
    }
}
